


/*
注意: int a=1; int b=2;  a/b=0.5  求商则要 a~/b;   表示a除以b，返回值是整形(向下取整);

字符串转小数: double.tryParse('3.14'); // 3.14
字符串转小数: double.tryParse('3'); // 3.0
dynamic 转数字: double.tryParse('${dynamic}');

 */
import 'dart:math';
import 'package:decimal/decimal.dart';
import 'package:intl/intl.dart';

abstract class XNumberUtils{

  //字符串转整数(默认10进制,四舍五入),可返回null,   var a=XNumberUtils.string2int('10',radix:8);
  //不能转小数的字符串
  static int? string2int(String num,{int? radix}){
    return int.tryParse(num,radix:radix);
  }
  /*
  字符串转小数 (通用)
  var value = double.tryParse('3.14'); // 3.14
  value = double.tryParse('  3.14 \xA0'); // 3.14
  value = double.tryParse('0.'); // 0.0
  value = double.tryParse('.0'); // 0.0
  value = double.tryParse('-1.e3'); // -1000.0
  value = double.tryParse('1234E+7'); // 12340000000.0
  value = double.tryParse('+.12e-9'); // 1.2e-10
  value = double.tryParse('-NaN'); // NaN
  value = tryParsedouble.('0xFF'); // null
  value = double.tryParse(double.infinity.toString()); // Infinity
  value = double.tryParse('123); // 123.0
   */
  static double? string2double(String num){
    return double.tryParse(num);
  }

  static double? dynamic2double(dynamic num){
    return double.tryParse('$num');
  }
  static int dynamic2int(dynamic num){
    return d2int(double.tryParse('$num')??0);
  }

  //取整(四舍五入)
  static int d2int(double d){
    //四舍五入取整(返回double)
    //var downNum = num.roundToDouble();//3.0
    //四舍五入取整(返回int)
    return d.round();
  }
  //向上取整
  static int d2int_ceil(double d){
    //向上取整（返回double）
    //var upperNum = num.ceilToDouble();//4.0
    //向上取整（返回int）
    return d.ceil();
  }
  //向下取整
  static int d2int_floor(double d){
    //向下取整(返回double)
    //var downNum = num.truncateToDouble();//3.0
    //向下取整(返回int)
    return d.truncate();
  }
  //保留n位小数(四舍五入) 如果是整数,d=i*1.0
  static double d2d(int n,double d){
    return string2double(d.toStringAsFixed(n))!;
  }


  //随机数
  static final Random _random=Random();
  static int randomInt(int max){
    return _random.nextInt(max);
  }
  // Value is >= 0.0 and < 1.0.
  static double randomDouble(){
    return _random.nextDouble();
  }
  static bool randomBool(){
    return _random.nextBool();
  }

/*
  给数字添加前置0:
  方法1:
  print(1.toString().padLeft(3, '0')); // 001
print(12.toString().padLeft(3, '0')); // 012
print(123.toString().padLeft(3, '0')); // 123
print(1234.toString().padLeft(3, '0')); // 1234
方法2:
NumberFormat formatter = NumberFormat("000");
print(formatter.format(1)); // 001
print(formatter.format(12)); // 012
print(formatter.format(123)); // 123
print(formatter.format(1234)); // 1234
   */
  static String number2StringFillZero(dynamic num){
    NumberFormat formatter = NumberFormat("00");
    return formatter.format(num);
  }


  //如果是整数则保留整数,如果是小数,则保留有效位数,最后为0的去除
  static String formatDoubleX(
      dynamic num,
      {
        bool isUseWanUnit=false,//是否以万为单位
        int? n,//保留多少位小数
      }
      )
  {
    //
    double r=dynamic2double(num)??0.0;
    if(isUseWanUnit && r>=10000){
      //使用万单位

      if(n!=null){
        //保留n位小数
        return '${formatDoubleN(n, r/10000)}万';
      }else{
        //默认2位,并去除末尾0
        r=d2d(2, r/10000);
        String result = r.toString().replaceAll(RegExp(r"([.]*0)(?!.*\d)"), "");
        return '$result万';
      }

    }else{
      //
      if(n!=null){
        //保留n位小数
        return formatDoubleN(n,r);
      }else{
        //去除最后的0
        String str=r.toString();
        String result = str.replaceAll(RegExp(r"([.]*0)(?!.*\d)"), "");
        return result;
      }

    }


  }

  /*
  保留多少位小数字符串,不足末尾补0
  NumberFormat formatter = NumberFormat("0.00");
  XLogUtils.printLog(formatter.format((-1.7734971743E7)));
   */
  static String formatDoubleN(int n,dynamic num){
    double d=dynamic2double(num)??0.0;
    return d.toStringAsFixed(n);
  }

  //绝对值
  static num abs(num n){
    return n.abs();
  }


  /*
  精度问题:
  https://pub-web.flutter-io.cn/packages/decimal
  dependencies:
    decimal: ^3.0.2
  import 'package:decimal/decimal.dart';


var currencyFormatter = DecimalFormatter(NumberFormat.simpleCurrency(name: 'USD'));
currencyFormatter.parse('\$3.14'); // => 3.14


var value = Decimal.parse('1234.56');
  var formatter = DecimalFormatter(NumberFormat.decimalPattern('en-US'));
  print(formatter.format(value));

extension on Decimal {
  String formatWith(NumberFormat formatter) => formatter.format(DecimalIntl(this));
}
main() {
  var value = Decimal.parse('1234.56');
  var formatter = DecimalFormatter(NumberFormat.decimalPattern('en-US'));
  print(value.formatWith(formatter));
}

0.1+0.2
   */
  static double add(dynamic num1,dynamic num2){
    Decimal res=(Decimal.tryParse('$num1')??Decimal.zero) + (Decimal.tryParse('$num2')??Decimal.zero);
    return res.toDouble();
  }
  static double sub(dynamic num1,dynamic num2){
    Decimal res=(Decimal.tryParse('$num1')??Decimal.zero) - (Decimal.tryParse('$num2')??Decimal.zero);
    return res.toDouble();
  }
//乘法(注意:不支持除法 /)
  static double mul(dynamic num1,dynamic num2){
    Decimal res=(Decimal.tryParse('$num1')??Decimal.zero) * (Decimal.tryParse('$num2')??Decimal.zero);
    return res.toDouble();
  }



}
